﻿
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class Reports_report_stp_sub_menu2
    Inherits System.Web.UI.Page
    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        CrystalReportViewer1.ReportSource = report
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As stp_sub_menuTableAdapters.USP_List_stp_sub_menuTableAdapter
        Dim dt As DataTable

        Dim strMainSystemCode As String
        Dim strMenuSystemCode As String
        Dim strSubMenuStatus As String
        Dim str_strMainSystemCode As String
        Dim str_strMenuSystemCode As String
        Dim str_SubMenuStatus As String
        Try
            'get Querystring
            strMainSystemCode = Request.QueryString("main_system_code")
            strMenuSystemCode = Request.QueryString("menu_system_code")
            str_strMainSystemCode = Request.QueryString("str_main_system_code")
            str_strMenuSystemCode = Request.QueryString("str_menu_system_code")
            strSubMenuStatus = Request.QueryString("sub_menu_status")
            str_SubMenuStatus = Request.QueryString("str_sub_menu_status")

            'check status
            str_strMainSystemCode = IIf(str_strMainSystemCode = "ระบบงาน", "ทั้งหมด", str_strMainSystemCode)
            str_strMenuSystemCode = IIf(str_strMenuSystemCode = "เมนูระบบงาน", "ทั้งหมด", str_strMenuSystemCode)
            str_SubMenuStatus = IIf(str_SubMenuStatus = "สถานะ", "ทั้งหมด", str_SubMenuStatus)

            dw = New stp_sub_menuTableAdapters.USP_List_stp_sub_menuTableAdapter
            'Retrieve data from DB & collect to datatable
            dt = dw.GetData(strMainSystemCode, strMenuSystemCode, strSubMenuStatus)

            'check 
            If (dt.Rows.Count > 0) Then
                'Create report engine
                'Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument

                'map report parth
                report.Load(Server.MapPath("report_stp_sub_menu.rpt"))

                'set data table to report
                report.SetDataSource(dt)

                'set input paratmenter
                report.SetParameterValue("SystemName", str_strMainSystemCode)
                report.SetParameterValue("MenuName", str_strMenuSystemCode)
                report.SetParameterValue("Status", str_SubMenuStatus)

                'Show report
                ' CrystalReportViewer1.ReportSource = report
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If
                ' TODO:
                ' Redirect to pervious page
            End If
        Catch ex As Exception
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub
End Class
